package com.ddstation.waste_delivery_api.controller;

import com.ddstation.common.access_token.service.AccessTokenService;
import com.ddstation.waste_delivery_api.model.WasteDeliveryApiInModel;
import com.ddstation.waste_delivery_api.service.WasteDeliveryYunbangAPIService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class WasteDeliveryYunbangAPIController {

	/**
	 * 数据传输接口
	 * 
	 * @return
	 * @throws ParseException 
	 */
	@RequestMapping(value="qztYBWasteDeliveryYunban.do/transfer", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> transferWasteDeliveryEvent(
			@RequestBody List<WasteDeliveryApiInModel> _data,
			HttpServletRequest request ) {

		Map<String, Object> ret = new HashMap<String, Object>();
		ret.put("errorcode", 0);
		ret.put("errormsg", "成功！");

		String sToken = request.getParameter(SHORT_ACCESS_TOKEN);
		if (StringUtils.isBlank(sToken)) {
			ret.put("errorcode","40001");
			ret.put("errormsg","token不合法：http header 中没有token数据");
			return ret;
		} else if (!accessTokenService.validateShortToken(sToken)) {
			ret.put("errorcode","40001");
			ret.put("errormsg","token不合法：token无效，请重新获取");
			return ret;
		}

		String insertRet = wasteDeliveryYunbangAPIService.insertList(_data);
		if ( "".equals( insertRet )){
			ret.put("errormsg","处理成功");
		} else {
			ret.put("errorcode","40002");
			ret.put("errormsg",insertRet);
		}
		return ret;
	}


	/**
	 * 数据传输接口
	 *
	 * @return
	 * @throws ParseException
	 */
	//TODO 更新接口
	@RequestMapping(value="qztYBWasteDeliveryYunban.do/transferUpd", method = RequestMethod.POST)
	public @ResponseBody Map<String, Object> transferWasteDeliveryUpdEvent(
			@RequestBody List<WasteDeliveryApiInModel> _data,
			HttpServletRequest request ) {

		Map<String, Object> ret = new HashMap<String, Object>();
		ret.put("errorcode", 0);
		ret.put("errormsg", "成功！");

		String sToken = request.getParameter(SHORT_ACCESS_TOKEN);
		if (StringUtils.isBlank(sToken)) {
			ret.put("errorcode","40001");
			ret.put("errormsg","token不合法：http header 中没有token数据");
			return ret;
		} else if (!accessTokenService.validateShortToken(sToken)) {
			ret.put("errorcode","40001");
			ret.put("errormsg","token不合法：token无效，请重新获取");
			return ret;
		}

		String insertRet = wasteDeliveryYunbangAPIService.insertList(_data);
		if ( "".equals( insertRet )){
			ret.put("errormsg","处理成功");
		} else {
			ret.put("errorcode","40002");
			ret.put("errormsg",insertRet);
		}
		return ret;
	}

	private final String SHORT_ACCESS_TOKEN = "short-access-token";

	@Inject private WasteDeliveryYunbangAPIService wasteDeliveryYunbangAPIService;
	@Inject private AccessTokenService accessTokenService = null;
}
